package com.manager.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manager.mapper.TbCommodityMapper;
import com.manager.pojo.TbCommodity;
import com.manager.pojo.TbCommodityExample;
import com.manager.pojo.TbOutlibrary;
import com.manager.pojo.TbCommodityExample.Criteria;
import com.manager.service.StorageService;
import com.manager.vo.TbUserEx;
/**
 * 
 * @Title:  StorageServiceimpl.java     
 * @Description:    库存Service实现类
 * @author:      
 * @date:   2019年2月16日 下午5:00:01   
 * @version V1.0
 */
@Service
public class StorageServiceimpl implements StorageService {

	@Autowired
	private TbCommodityMapper tbCommodityMapper;
	
	@Override		//获取库存分页list
	public TbUserEx getStorageList(Integer page, Integer rows)  {

		TbUserEx tbUserEx = new TbUserEx();
		List<TbCommodity> list = null;
		TbCommodityExample example = new TbCommodityExample();
		
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andSStorageNotEqualTo(0);
		//设置分页范围
		PageHelper.startPage(page,rows);
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("库存列表获取异常");
			return tbUserEx;
		}
		
		if(list == null || list.size()<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("库存列表获取失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);	
		tbUserEx.setCode(200);
		tbUserEx.setMsg("库存列表数据获取成功");
		
		
		return tbUserEx;
	}
	
	
	

	@Override		//添加到库存
	public TbUserEx addStorage(TbCommodity tbCommodity) throws Exception {

		TbUserEx tbUserEx = new TbUserEx();
		List<TbCommodity> list = null;
		Integer result = 0;
		TbCommodityExample example = new TbCommodityExample();
		
		Criteria criteria = example.createCriteria();
		criteria.andNameLike(tbCommodity.getName());
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			throw new Exception();
		}
		
		if(list.size()>0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("类似的商品已存在");
			return tbUserEx;
		}
		
		try {
			result = tbCommodityMapper.insertSelective(tbCommodity);
		}catch(Exception e) {
			throw new Exception();
		}
		
		if(result<0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("商品添加失败");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("商品添加成功");
		
		return tbUserEx;
	}

	@Override		//根据ID删除库存
	public TbCommodity getTbCommodityById(Integer id)  {
		
		TbCommodity tbCommodity = null;
		try {
			tbCommodity = tbCommodityMapper.selectByPrimaryKey(id);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return tbCommodity;
	}

	@Override			//记录是已存在情况下不修改
	public TbUserEx editStorage(TbCommodity tbCommodity) throws Exception {
		
		TbUserEx tbUserEx = new TbUserEx();
		List<TbCommodity> list = null;
		
		
		TbCommodityExample example = new TbCommodityExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameLike(tbCommodity.getName());
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			throw new Exception();
		}
		
		if(list.size()>0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("类似的商品已存在");
			return tbUserEx;
		}
		
		Integer result = 0;
		try {
			result = tbCommodityMapper.updateByPrimaryKeySelective(tbCommodity);
		}catch(Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
		
		if(result<0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("商品添加失败");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("商品添加成功");
		
		return tbUserEx;
	}

	@Override		//根据id删除库存
	public Boolean delStorage(Integer id) throws Exception {
		
		Integer result = 0;
		
		try {
			result = tbCommodityMapper.deleteByPrimaryKey(id);
		}catch(Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
		
		return result>0?true:false;
	}

	@Override
	public Boolean outStorage(TbCommodity tbCommodity) throws Exception {
		return null;
	}

	@Override			//获取入库分页信息
	public TbUserEx getInLibraryList(Integer page, Integer rows) throws Exception {
		
		TbUserEx tbUserEx = new TbUserEx();
		List<TbCommodity> list = null;
		TbCommodityExample example = new TbCommodityExample();
		
		example.setOrderByClause("id desc");
		Criteria createCriteria = example.createCriteria();
		//设置分页范围
		PageHelper.startPage(page,rows);
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			throw new Exception();
		}
		
		if(list.size()<=0 || list == null) {
			tbUserEx.setCode(400);
			tbUserEx.setError("库存列表获取失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);	
		tbUserEx.setCode(200);
		tbUserEx.setMsg("库存列表数据获取成功");
		
		return tbUserEx;
	}


	@Override			//获取出库分页信息
	public TbUserEx getOutLibraryList(Integer page, Integer rows) throws Exception {
		
		TbUserEx tbUserEx = new TbUserEx();
		List<TbCommodity> list = null;
		TbCommodityExample example = new TbCommodityExample();
		
		example.setOrderByClause("id desc");
		Criteria createCriteria = example.createCriteria();
		//设置分页范围
		PageHelper.startPage(page,rows);
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			throw new Exception();
		}
		
		if(list.size()<=0 || list == null) {
			tbUserEx.setCode(400);
			tbUserEx.setError("库存列表获取失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);	
		tbUserEx.setCode(200);
		tbUserEx.setMsg("库存列表数据获取成功");
		
		return tbUserEx;
	}


	@Override			//根据主键选择性更新库存的缺货警告值
	public Boolean updataSetLessOrMany(TbCommodity tbCommodity) throws Exception {
		
		Integer result = 0;
		try {
			result = tbCommodityMapper.updateByPrimaryKeySelective(tbCommodity);
		}catch(Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
		
		return result>0?true:false;
	}

	@Override		//获取缺货警告分页信息
	public TbUserEx getLessList(Integer page, Integer rows) {
		
		TbUserEx tbUserEx = new TbUserEx();
		List<TbCommodity> list = null;
		TbCommodityExample example = new TbCommodityExample();
		
		example.setOrderByClause("id desc");
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("缺货的列表数据获取异常");
			return tbUserEx;
		}
		
		if(list.size()<=0 & list ==null) {
			tbUserEx.setCode(400);
			tbUserEx.setError("缺货的列表数据获取失败");
			return tbUserEx;
		}
		
		example = new TbCommodityExample();
		Criteria createCriteria = example.createCriteria();
		//设置分页范围
		PageHelper.startPage(page,rows);
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			tbUserEx.setCode(500);
			tbUserEx.setError("缺货的列表数据获取异常");
			return tbUserEx;
		}
		
		if(list.size()<=0 || list == null) {
			tbUserEx.setCode(400);
			tbUserEx.setError("缺货的列表数据获取失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);	
		tbUserEx.setCode(200);
		tbUserEx.setMsg("缺货列表数据获取成功");
		
		return tbUserEx;
	}

	@Override			//获取滞销获取库存分页信息
	public TbUserEx getManyList(Integer page, Integer rows) throws Exception {
		
		TbUserEx tbUserEx = new TbUserEx();
		List<TbCommodity> list = null;
		TbCommodityExample example = new TbCommodityExample();
		
		example.setOrderByClause("id desc");
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			throw new Exception();
		}
		
		if(list.size()<=0 & list ==null) {
			tbUserEx.setCode(400);
			tbUserEx.setError("滞销的列表数据获取失败");
			return tbUserEx;
		}
		
		example = new TbCommodityExample();
		Criteria createCriteria = example.createCriteria();
		//设置分页范围
		PageHelper.startPage(page,rows);
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			throw new Exception();
		}
		
		if(list.size()<=0 || list == null) {
			tbUserEx.setCode(400);
			tbUserEx.setError("滞销的列表数据获取失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);	
		tbUserEx.setCode(200);
		tbUserEx.setMsg("滞销列表数据获取成功");
		
		return tbUserEx;
	}



	@Override		//添加入库信息之后递增库存数量 函数
	public TbUserEx saveStorage(TbCommodity tbCommodity) {

		TbUserEx tbUserEx = new TbUserEx();
		Integer result = 0;
		try {
			result = tbCommodityMapper.updateByPrimaryKeySelective(tbCommodity);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		if(result<=0){
			tbUserEx.setCode(400);
			tbUserEx.setError("库存信息递增失败");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("库存信息递增成功");
		
		return tbUserEx;
	}

	
	@Override		//获取所有分页信息
	public TbUserEx getStorageList() {
		
		TbUserEx tbUserEx = new TbUserEx();
		
		TbCommodityExample example = new TbCommodityExample();
		example.setOrderByClause("id desc");
		com.manager.pojo.TbCommodityExample.Criteria criteria = example.createCriteria();
		
		List<TbCommodity>list = null;
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("入库列表获取异常");
			return tbUserEx;
		}
		
		if(list.size()<=0 || list == null) {
			tbUserEx.setCode(500);
			tbUserEx.setError("入库列表没有信息");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("入库列表获取成功");
		tbUserEx.setRows(list);
		
		return tbUserEx;
	}


	@Override			//根据姓名获取库存分页信息
	public TbCommodity getTbCommodityByName(String name) throws Exception {
		
		TbCommodityExample example = new TbCommodityExample();
		List<TbCommodity> list = null;
		
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(name);
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
		}

		return list.size()>0?list.get(0):null;
	}


	@Override			//记录已存在的情况下进行修改
	public TbUserEx editExisStorage(TbCommodity tbCommodity) throws Exception {
		
		TbUserEx tbUserEx = new TbUserEx();
		List<TbCommodity> list = null;
		TbCommodityExample example = new TbCommodityExample();
		
		Criteria criteria = example.createCriteria();
		criteria.andNameLike(tbCommodity.getName());
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			throw new Exception();
		}
		
		if(list.size()<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("类似的商品不在");
			return tbUserEx;
		}
		
		Integer result = 0;
		try {
			result = tbCommodityMapper.updateByPrimaryKeySelective(tbCommodity);
		}catch(Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
		
		if(result<0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("商品添加失败");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("商品添加成功");
		
		return tbUserEx;
	}


	@Override			//根据时间范围查库存
	public TbUserEx getStorageListByDateTime(Integer page, Integer rows, Date start, Date end) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbCommodityExample example = new TbCommodityExample();
		List<TbCommodity>list = null;
		
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andDtimeBetween(start, end);
		//设置分页范围
		PageHelper.startPage(page, rows);
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("按时间查询异常");
			return tbUserEx;
		}
		
		if(list==null || list.size()<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("按时间查询失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		tbUserEx.setCode(200);
		tbUserEx.setMsg("按时间查询成功");
		
		return tbUserEx;
	}


	@Override		//按商品名称查询
	public TbUserEx getStorageListByName(Integer page, Integer rows, String like) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbCommodityExample example = new TbCommodityExample();
		List<TbCommodity>list = null;
		
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(like);
		//设置分页范围
		PageHelper.startPage(page, rows);
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("按商品名称查询异常");
			return tbUserEx;
		}
		
		if(list==null || list.size()<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("按商品名称查询失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		tbUserEx.setCode(200);
		tbUserEx.setMsg("按商品名称查询成功");
		
		return tbUserEx;
	}


	@Override			//按商品分类查询
	public TbUserEx getStorageListByType(Integer page, Integer rows, String like) {

		TbUserEx tbUserEx = new TbUserEx();
		TbCommodityExample example = new TbCommodityExample();
		List<TbCommodity>list = null;
		
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andTypeEqualTo(like);
		//设置分页范围
		PageHelper.startPage(page, rows);
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("按商品分类查询异常");
			return tbUserEx;
		}
		
		if(list==null || list.size()<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("按商品分类查询失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		tbUserEx.setCode(200);
		tbUserEx.setMsg("按商品分类查询成功");
		
		return tbUserEx;
	}


	@Override				//按商品的SKU查询
	public TbUserEx getStorageListBySKU(Integer page, Integer rows, String like) {

		TbUserEx tbUserEx = new TbUserEx();
		TbCommodityExample example = new TbCommodityExample();
		List<TbCommodity>list = null;
		
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andSkuEqualTo(like);
		//设置分页范围
		PageHelper.startPage(page, rows);
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("按商品SKU查询异常");
			return tbUserEx;
		}
		
		if(list==null || list.size()<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("按商品SKU查询失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		tbUserEx.setCode(200);
		tbUserEx.setMsg("按商品SKU查询成功");
		
		return tbUserEx;
	}

	
	
	@Override			//根据时间获取分页信息
	public TbUserEx getStorageLists(Date start,Date end) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbCommodityExample example = new TbCommodityExample();
		List<TbCommodity>list = null;
		
		example.setOrderByClause("id desc");
		com.manager.pojo.TbCommodityExample.Criteria criteria = example.createCriteria();
		criteria.andSStorageNotEqualTo(0);
		criteria.andDtimeBetween(start, end);
		
		try {
			list = tbCommodityMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("库存列表获取异常");
			return tbUserEx;
		}
		
		if(list.size()<=0 || list == null) {
			tbUserEx.setCode(500);
			tbUserEx.setError("库存列表没有信息");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("库存列表获取成功");
		tbUserEx.setRows(list);
		
		return tbUserEx;
	}

}
